﻿using System;
using System.Data;
using System.Data.Common;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using eShop.DataAccess.Entity;

namespace eShop.DataAccess
{
    public partial class CityDAL : BaseDAL
    {
        public static bool Insert(City city)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("City_InsertOrUpdate");
            db.AddOutParameter(dbCommand, "Id", DbType.Int32, 4);
            db.AddInParameter(dbCommand, "StateId", DbType.Int32, city.StateId);
            db.AddInParameter(dbCommand, "Name", DbType.String, city.Name);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, city.IsActive);

            if (db.ExecuteNonQuery(dbCommand) > 0)
            {
                city.Id = Convert.ToInt32(db.GetParameterValue(dbCommand, "@Id"));
                return true;
            }
            return false;
        }

        public static bool Update(City city)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("City_InsertOrUpdate");
            db.AddInParameter(dbCommand, "Id", DbType.Int32, city.Id);
            db.AddInParameter(dbCommand, "StateId", DbType.Int32, city.StateId);
            db.AddInParameter(dbCommand, "Name", DbType.String, city.Name);
            db.AddInParameter(dbCommand, "IsActive", DbType.Boolean, city.IsActive);

            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        public static List<City> GetAll(Boolean allowPaging = false, Int32 pageIndex = 0, Int32 pageSize = 0, String orderColumn = "Id", Boolean isAscending = false)
        {
            List<City> cityList = new List<City>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("City_GetAll");
            db.AddInParameter(dbCommand, "AllowPaging", DbType.Boolean, allowPaging);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, pageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, pageSize);
            db.AddInParameter(dbCommand, "OrderColumn", DbType.String, orderColumn);
            db.AddInParameter(dbCommand, "IsAscending", DbType.Boolean, isAscending);
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                cityList = GetEntitiesFromReader(dataReader);
            }
            return cityList;
        }

        public static List<City> GetByKeys(Int32? id, Int32? stateId)
        {
            List<City> cityList = new List<City>();
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("City_GetByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (stateId.HasValue)
                db.AddInParameter(dbCommand, "StateId", DbType.Int32, stateId);

            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                cityList = GetEntitiesFromReader(dataReader);
            }
            return cityList;
        }

        public static bool DeleteByKeys(Int32? id, Int32? stateId)
        {
            Database db = GetDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("City_DeleteByKeys");
            if (id.HasValue)
                db.AddInParameter(dbCommand, "Id", DbType.Int32, id);
            if (stateId.HasValue)
                db.AddInParameter(dbCommand, "StateId", DbType.Int32, stateId);
            if (db.ExecuteNonQuery(dbCommand) > 0)
                return true;
            return false;
        }

        private static List<City> GetEntitiesFromReader(IDataReader dataReader)
        {
            List<City> cityList = new List<City>();
            while (dataReader.Read())
            {
                cityList.Add(GetEntityFromReader(dataReader));
            }
            return cityList;
        }

        private static City GetEntityFromReader(IDataReader dataReader)
        {
            City city = new City();
            city.Id = (Int32)dataReader["Id"];
            city.StateId = (Int32)dataReader["StateId"];
            city.Name = (String)dataReader["Name"];
            city.IsActive = (Boolean)dataReader["IsActive"];
            return city;
        }
    }
}

